package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Bean;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
    private List<Bean> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    public SearchAdapter(List<Bean> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position, int focus,int userid); //传递boolean类型数据给activity
    }

    SearchAdapter.OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(SearchAdapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    public static interface OnAddClickListener2 {
        // true add; false cancel
        void onItemClicks2(int userId);
    }

    SearchAdapter.OnAddClickListener2 onItemAddClick2;

    public void setOnAddClickListener2(SearchAdapter.OnAddClickListener2 onItemAddClick2) {
        this.onItemAddClick2 = onItemAddClick2;
    }

    @NonNull
    @Override
    public SearchAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_search, parent, false);
        SearchAdapter.ViewHolder holder = new SearchAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull SearchAdapter.ViewHolder holder, @SuppressLint("RecyclerView")int position) {
        Bean bean = list.get(position);
        Glide.with(MyAppliaction.getContext())
                .load(url + bean.getIcon())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.im_search);
        holder.tx1_search.setText(bean.getName());
        holder.tx2_search.setText(bean.getPersionSign());
        if (bean.getFocus() == 0) {
            holder.bt_search.setText(R.string.mi_guanzhu);
            holder.bt_search.setBackgroundResource(R.drawable.shape_search_follow1);
        } else {
            holder.bt_search.setText(R.string.mi_yiguanzhu);
            holder.bt_search.setBackgroundResource(R.drawable.shape_search_follow2);
        }
        holder.bt_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(bean.getFocus() == 0){
                    bean.setFocus(1);
                    holder.bt_search.setText(R.string.mi_yiguanzhu);
                    holder.bt_search.setBackgroundResource(R.drawable.shape_search_follow2);
                    notifyDataSetChanged();
                }else {
                    bean.setFocus(0);
                    holder.bt_search.setText(R.string.mi_guanzhu);
                    holder.bt_search.setBackgroundResource(R.drawable.shape_search_follow1);
                    notifyDataSetChanged();
                }
                onItemAddClick.onItemClicks(position, bean.getFocus(),bean.getUserId());
            }
        });
        holder.im_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick2.onItemClicks2(bean.getUserId());
            }
        });
        if(bean.getIdentity()==10){
            holder.im2_vip.setImageResource(R.mipmap.icon_huangjin);
        }else if(bean.getIdentity()==20){
            holder.im2_vip.setImageResource(R.mipmap.icon_bojin);
        }else if(bean.getIdentity()==30){
            holder.im2_vip.setImageResource(R.mipmap.icon_zuanshi);
        }else {
            holder.im2_vip.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im_search,im2_vip;
        private TextView tx1_search, tx2_search;
        private Button bt_search;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            im2_vip = itemView.findViewById(R.id.im2_vip);
            im_search = itemView.findViewById(R.id.im_search);
            tx1_search = itemView.findViewById(R.id.tx1_search);
            tx2_search = itemView.findViewById(R.id.tx2_search);
            bt_search = itemView.findViewById(R.id.bt_search_follow);
        }
    }
}
